package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.eiahe.hr.attendance.domain.bo.OvertimeRecordBo;
import org.eiahe.hr.attendance.domain.vo.OvertimeRecordVo;
import org.eiahe.hr.attendance.service.IOvertimeRecordService;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 加班记录管理
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/overtimeRecord")
public class OvertimeRecordController extends BaseController {

    private final IOvertimeRecordService overtimeRecordService;

    /**
     * 查询加班记录列表
     */
    @SaCheckPermission("attendance:overtimeRecord:list")
    @GetMapping("/list")
    public TableDataInfo<OvertimeRecordVo> list(OvertimeRecordBo bo, PageQuery pageQuery) {
        return overtimeRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出加班记录列表
     */
    @SaCheckPermission("attendance:overtimeRecord:export")
    @Log(title = "加班记录", businessType = BusinessType.EXPORT, linkType = "overtime")
    @PostMapping("/export")
    public void export(OvertimeRecordBo bo, HttpServletResponse response) {
        List<OvertimeRecordVo> list = overtimeRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "加班记录", OvertimeRecordVo.class, response);
    }

    /**
     * 获取加班记录详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("attendance:overtimeRecord:query")
    @GetMapping("/{recordId}")
    public R<OvertimeRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long recordId) {
        return R.ok(overtimeRecordService.queryById(recordId));
    }

    /**
     * 新增加班记录(手动申请)
     */
    @SaCheckPermission("attendance:overtimeRecord:add")
    @Log(title = "加班申请", businessType = BusinessType.INSERT, linkType = "overtime")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OvertimeRecordBo bo) {
        return toAjax(overtimeRecordService.insertByBo(bo));
    }

    /**
     * 修改加班记录
     */
    @SaCheckPermission("attendance:overtimeRecord:edit")
    @Log(title = "加班记录", businessType = BusinessType.UPDATE, linkType = "overtime")
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OvertimeRecordBo bo) {
        return toAjax(overtimeRecordService.updateByBo(bo));
    }

    /**
     * 删除加班记录
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("attendance:overtimeRecord:remove")
    @Log(title = "加班记录", businessType = BusinessType.DELETE, linkType = "overtime")
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] recordIds) {
        return toAjax(overtimeRecordService.deleteWithValidByIds(List.of(recordIds)));
    }

    /**
     * 30天内加班异常
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/listMonth")
    public TableDataInfo<OvertimeRecordVo> listMonth(OvertimeRecordBo bo, PageQuery pageQuery) {
        return overtimeRecordService.listMonth(bo, pageQuery);
    }
}
